﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SignalrDemo.Hubs;
using Microsoft.AspNet.SignalR;
using SignalrDemo.ViewModels;

namespace SignalrDemo.Controllers
{
    public class PushController : Controller
    {
        //
        // GET: /Push/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Push(string msg)
        {
            AjaxResult result = new AjaxResult();
            try
            {
                IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<PushHub>();
                chat.Clients.All.notice(msg);
                result.Message = "广播成功!";
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "广播失败!";
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [ValidateInput(false)]
        public ActionResult PushGroup(string group,string msg)
        {
        	var result=new AjaxResult();
        	try 
        	{
        		IHubContext chat=GlobalHost.ConnectionManager.GetHubContext<PushHub>();
        		chat.Clients.Group(group).notice(msg);
        		result.IsSuccess=true;
        		
        	} catch (Exception) {
        		result.IsSuccess=false;
        	}
        	return Json(result,JsonRequestBehavior.AllowGet);
        }

    }
}
